<script setup>
import { useArctileStore } from '@/stores'
import { onMounted } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import { ref } from 'vue'
import ChannelEdit from './components/ChannelEdit.vue'
import { artDeleteChannelService } from '@/api/article.js'

const articleStore = useArctileStore()
const channelList = ref([])
const isLoading = ref(false)

const getChannelList = async () => {
  isLoading.value = true
  await articleStore.getChannelList()
  isLoading.value = false
  channelList.value = articleStore.channelList
}

onMounted(() => {
  getChannelList()
})

const dialog = ref()
const handleAdd = () => {
  dialog.value.open({})
}
function handleEdit(row) {
  dialog.value.open(row)
}
const handleDelete = async (row) => {
  await ElMessageBox.confirm('你确定要删除这条分类吗', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
  await artDeleteChannelService(row.id)
  ElMessage({
    type: 'success',
    message: '删除成功'
  })
  //数据回显
  getChannelList()
}

//处理更新事件回显
const handleSuccess = async () => {
  getChannelList()
}
</script>
<template>
  <page-container title="文章分类">
    <template #extra>
      <el-button type="primary" @click="handleAdd">文章分类</el-button>
    </template>
    <el-table :data="channelList" style="width: 100%" v-loading="isLoading">
      <el-table-column label="序号" width="100" type="index"> </el-table-column>
      <el-table-column label="分类名称" prop="cate_name"></el-table-column>
      <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="scope">
          <el-button
            type="primary"
            :icon="Edit"
            circle
            plain
            @click="handleEdit(scope.row)"
          />
          <el-button
            type="danger"
            :icon="Delete"
            circle
            plain
            @click="handleDelete(scope.row)"
          />
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>
    </el-table>
    <channel-edit ref="dialog" @success="handleSuccess"></channel-edit>
  </page-container>
</template>
